package com.daily.pump.dialog;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.daily.pump.R;
import com.daily.pump.adapter.UserListAdapter;
import com.daily.pump.dao.DaoSharedPreferences;
import com.daily.pump.model.Device;

import java.util.List;


/**
 * Desc:添加设备弹框
 * <p>
 * Author: [李豫]
 * Date: 2018-08-02
 * Copyright: Copyright (c) 2010-2018
 * Updater:
 * Update Time:
 * Update Comments:
 */
public class AddDeviceDialog extends DialogFragment {

    private UserListAdapter mListAdapter;

    private AddDeviceDialogListener mAddDeviceDialogListener;

    public void setAddDeviceDialogListener(AddDeviceDialogListener listener) {
        this.mAddDeviceDialogListener = listener;
    }

    public static AddDeviceDialog newInstance() {
        AddDeviceDialog scanResultNextDialog = new AddDeviceDialog();
        return scanResultNextDialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.customDialog);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getDialog().setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                dismissAllowingStateLoss();
            }
        });
        Window window = getDialog().getWindow();
        window.setGravity(Gravity.TOP | Gravity.RIGHT);
        window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View dialogView = inflater.inflate(R.layout.dialog_add_device, container, false);
        initData(dialogView);
        return dialogView;
    }

    private void initData(View dialogView) {
        RecyclerView mHistoryRv = dialogView.findViewById(R.id.history_rv);
        mListAdapter = new UserListAdapter(getActivity());
        mListAdapter.setWifiListAdapterListener(new UserListAdapter.WifiListAdapterListener() {
            @Override
            public void onItemClick(Device device, int position) {
                Device connDevice = mListAdapter.setSelect(position);
                if (mAddDeviceDialogListener != null) {
                    mAddDeviceDialogListener.onConnDevice(connDevice);
                }
            }

            @Override
            public void onItemLongClick(Device device, int position) {
                if (mAddDeviceDialogListener != null){
                    mAddDeviceDialogListener.onModifyDevice(device);
                }
            }
        });
        mHistoryRv.setLayoutManager(new LinearLayoutManager(getActivity()));
        mHistoryRv.setAdapter(mListAdapter);
        List<Device> wifiConfigList = DaoSharedPreferences.getInstance().getDeviceList();
        mListAdapter.setDataSource(wifiConfigList);
        mListAdapter.notifyDataSetChanged();

        dialogView.findViewById(R.id.add_device_iv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mAddDeviceDialogListener != null){
                    mAddDeviceDialogListener.onAddDevice();
                }
            }
        });
    }

    public void show(FragmentManager manager) {
        if (!isShowing()) {
            show(manager, AddDeviceDialog.class.getSimpleName());
        }
    }

    public boolean isShowing() {
        if (getDialog() != null && getDialog().isShowing()) {
            return true;
        }
        return false;
    }

    /**
     * Desc:
     * <p>
     * Author: [李豫]
     * Date: 2020-11-11
     */
    public void clearSelect() {
        if (mListAdapter != null){
            mListAdapter.clearSelect();
        }
    }

    /**
     * Desc:添加设备
     * <p>
     * Author: [李豫]
     * Date: 2020-11-11
     *
     * @param device
     */
    public void addDevice(Device device) {
        if (mListAdapter != null){
            mListAdapter.addDevice(device);
        }
    }

    public interface AddDeviceDialogListener {
        void onConnDevice(Device device);
        void onAddDevice();
        void onModifyDevice(Device device);
    }
}

